package GUI;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * Extend the return date of a specific borrowed copy
 * @author Benny
 *
 */
public class ExtendBorrowGUI extends JPanel {
	public JTextField textCopyID;
	public JTextField textReturnDate;
	public MainFrame mf;
	/**
	 * initialize all the text fields of the GUI
	 */
	public void init(){
		textCopyID.setText("");
		textReturnDate.setText("");
	}
	/**
	 * check for the validity of the specified copyID
	 * @return copyID validity status
	 */
	public int checkCopyID(){
		try  
		{  
			@SuppressWarnings("unused")
			int i = Integer.parseInt(textCopyID.getText());  
		}  
		catch(NumberFormatException nfe)  
		{  
			return 0;  
		}  
		return 1;  
	}
	
	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program 
	 */
	public ExtendBorrowGUI(MainFrame M) {
		setLayout(null);
		mf=M;
		JLabel lblExtendBorrowReturn = new JLabel("Extend Borrow Return Date");
		lblExtendBorrowReturn.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblExtendBorrowReturn.setBounds(166, 27, 253, 14);
		add(lblExtendBorrowReturn);
		
		JLabel lblCopyid = new JLabel("CopyID:");
		lblCopyid.setBounds(120, 81, 121, 14);
		add(lblCopyid);
		
		textCopyID = new JTextField();
		textCopyID.setBounds(269, 78, 137, 20);
		add(textCopyID);
		textCopyID.setColumns(10);
		
		JButton btnShowReturnDate = new JButton("Show Return Date");
		btnShowReturnDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<Object> send = new ArrayList<>();
				if(checkCopyID()==1)
				{
					int copyID=Integer.decode(textCopyID.getText());
					send.add("1");
					send.add("ShowReturnDate");
					send.add(copyID);
					mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "copyID must be a number", "Program", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnShowReturnDate.setBounds(124, 124, 162, 23);
		add(btnShowReturnDate);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(296, 124, 110, 23);
		add(btnClear);
		
		JLabel lblReturnDate = new JLabel("Return Date:");
		lblReturnDate.setBounds(120, 180, 121, 14);
		add(lblReturnDate);
		
		textReturnDate = new JTextField();
		textReturnDate.setEditable(false);
		textReturnDate.setBounds(269, 177, 137, 20);
		add(textReturnDate);
		textReturnDate.setColumns(10);
		
		JButton btnExtend = new JButton("Extend");
		btnExtend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<Object> send = new ArrayList<>();
				int copyID=Integer.decode(textCopyID.getText());
				Integer id=mf.logged_in_user.id;
				send.add("1");
				send.add("ExtendBorrow");
				send.add(id);
				send.add(copyID);
				mf.client.accept(send);
			}
		});
		btnExtend.setBounds(205, 225, 89, 23);
		add(btnExtend);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);

	}

}
